﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using eduscrum.Models;
using eduscrum.DAL;
using eduscrum.ViewModels;
using System.Web.Routing;
using System;

namespace eduscrum.Controllers
{ 
    public class PhaseController : Controller
    {
        private SystemContext db = new SystemContext();

        public ViewResult IndexProject(int projectID)
        {
            var phases = db.Phases.Where(p => p.Project.ProjectID == projectID).ToList();
            List<PhaseVM> phasesVM = AutoMapper.Mapper.Map<List<Phase>, List<PhaseVM>>(phases);
            return View(phasesVM);
        }

        //
        // GET: /Phase/Create

        public ViewResult Create(int projectID)
        {
            var viewModel = new PhaseVM { Project = new ProjectVM { ProjectID = projectID } };
            return View("Create", viewModel);
        } 

        //
        // POST: /Phase/Create

        [HttpPost]
        public ActionResult Create(PhaseVM phaseToCreate)
        {
            if (Request["ProjectID"] != null)
            {
                phaseToCreate.Project = new ProjectVM { ProjectID = Int32.Parse(Request["ProjectID"]) };
            }
            if (ModelState.IsValid)
            {
                int projectID = Int32.Parse(Request["ProjectID"]);
                Project projectToAddPhaseTo = db.Projects.Find(projectID);
                projectToAddPhaseTo.PhaseCount++;
                Phase newPhase = new Phase();
                AutoMapper.Mapper.Map(phaseToCreate, newPhase);
                newPhase.Project = projectToAddPhaseTo;
                db.Phases.Add(newPhase);
                //projectToAddPhaseTo.Phases.Add(newPhase);
                //db.Entry(projectToAddPhaseTo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Edit", new RouteValueDictionary( new { controller = "Project", action = "Edit", Id = projectToAddPhaseTo.ProjectID }));
            }
            return View(phaseToCreate);
        }
        
        //
        // GET: /Phase/Edit/5
 
        public ActionResult Edit(int id)
        {         
            Phase phaseToEdit = db.Phases.Find(id);
            PhaseVM viewModel = new PhaseVM();
            AutoMapper.Mapper.Map(phaseToEdit, viewModel);
            return View("Edit", viewModel);
        }

        //
        // POST: /Phase/Edit/5

        [HttpPost]


        public ActionResult Edit(PhaseVM phaseToEditVM)
        {
            if (Request["ProjectID"] != null)
            {
                phaseToEditVM.Project = new ProjectVM { ProjectID = Int32.Parse(Request["ProjectID"]) };
            }
            if (ModelState.IsValid && Request["ProjectID"] != null)
            {
                Phase phaseEdited = new Phase();
                AutoMapper.Mapper.Map(phaseToEditVM, phaseEdited);
                phaseEdited.Project = db.Projects.Find(Int32.Parse(Request["ProjectID"]));
                //db.Entry(phaseEdited.Project).State = EntityState.Modified;
                db.Entry(phaseEdited).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Edit", new RouteValueDictionary(new { controller = "Project", action = "Edit", Id = Int32.Parse(Request["ProjectID"]) }));
            }
            return View(phaseToEditVM);
        }

        //
        // GET: /Phase/Delete/5
 
        public ActionResult Delete(int id)
        {
            Phase phase = db.Phases.Find(id);
            int projectID = phase.Project.ProjectID;
            if (db.Phases.Count() > 1)
            {
                phase.Project.PhaseCount--;
                db.Entry(phase.Project).State = EntityState.Modified;
                db.Phases.Remove(phase);
                db.UserStories.Where(u => u.Phase.PhaseID == id).ToList().ForEach(u => u.Phase = null);
                db.SaveChanges();
                Phase phaseFirst = db.Phases.Where(p => p.Project.ProjectID == projectID).First();
                db.UserStories.Where(u => u.Phase == null).Where(u => u.Project.ProjectID == projectID).ToList().ForEach(u => u.Phase = phaseFirst);
                db.SaveChanges();
            }
            return RedirectToAction("Edit", new RouteValueDictionary(new { controller = "Project", action = "Edit", Id = projectID }));
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}